package catchData;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

/**
 * @author suwy
 * @date 2014-2-18
 * @todo TODO
 */
public class CatchYaozhi {

	private static final Logger LOG = Logger.getLogger(CatchYaozhi.class);
	private String webURL = "http://db.yaozh.com/index.php?action=duizhaopin&pre=duizhaopin&page=";
	private String htmFolder = "I:/data/新康界抓取数据/download/中检所标准品查询";
	private int pageNum = 75;
	
	public static void main(String[] args) {
		CatchYaozhi instance = new CatchYaozhi();
		instance.excute();
	}
	
	private void excute() {
		HttpClient client = CatchDataUtil.initHttpClient();
		PostMethod postMethod = null;
		StringBuilder stringBuilder = new StringBuilder();
		try {
			for(int i = 1; i <= pageNum; i++) {
				postMethod = new PostMethod(webURL + i);
				CatchDataUtil.setHeaders(postMethod, webURL + i,
						CatchDataUtil.getWebSite(webURL));
				client.executeMethod(postMethod);
				stringBuilder.append(postMethod.getResponseBodyAsString());
				CatchDataUtil.saveDataFile(htmFolder, stringBuilder.toString(), i, "utf-8");
				postMethod.releaseConnection();
				stringBuilder.delete(0, stringBuilder.length());
			}
		} catch (Exception e) {
			LOG.info(e);
		} finally {
			postMethod.releaseConnection();
		}
	}

}